using System.IO;
using Autofac;
using LocalOverride.Fiddler.Store;
using LocalOverride.Fiddler.UI;
using LocalOverride.Fiddler.UrlMapping;

namespace LocalOverride.Fiddler
{
    public static class ComponentBuilder
    {
        public static IContainer SetupAutoFac()
        {
            var localFileAccess = new LocalFileAccess();
            var store = new MappingStore(localFileAccess);
            var factory = new MappingRegistryFactory(store);

            var builder = new ContainerBuilder();
            builder.RegisterInstance(factory).SingleInstance();
            builder.RegisterInstance<IMappingStore>(store).SingleInstance();
            builder.RegisterType<Logger>().As<ILogger>().SingleInstance();
            builder.RegisterType<MappingEditor>().As<IMappingEditorView>().As<ILoggingView>().SingleInstance();
            builder.RegisterType<MappingEditorPresenter>();
            builder.RegisterType<LoggingPresenter>();
            return builder.Build();
        }
    }

    public class LocalFileAccess : ILocalFileAccess
    {
        public bool IsValidFilename(string path)
        {
            return File.Exists(path);
        }

        public bool IsValidDirectory(string path)
        {
            return Directory.Exists(path);
        }
    }
}